package Offer.offer;

/**
 * 连续子数组的最大和
 * @author leiwenkang
 * @date 2020/4/10
 */
public class Test31连续子数组的最大和 {
    public static void main(String[] args) {
        int[] array = {1,-2,3,10,-4,7,2,-5};
        int[] array1 = {-5,-4,-3,-2,-1};
        int[] array2 = {1,-2,5,-3,4};
        int[] array3 = {1,2,3,4,5,6};
        System.out.println(findGreatestOfSum(array));
    }

    public static int findGreatestOfSum(int[] array) {
        if (array == null || array.length == 0) {
            throw new RuntimeException("非法输入");
        }

        int sum = 0;
        int result = Integer.MIN_VALUE;
        for (int i = 0; i < array.length; i++) {
            if (sum <= 0) {
                sum = array[i];
            } else {
                sum += array[i];
            }

            if (sum > result) {
                result = sum;
            }
        }
        return result;
    }
}
